package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;


public class GetResult extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private AxdrEnum dar = null;
    private Data data = null;

    public GetResult() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public Choice getChoice() {
        return choice;
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setDar(AxdrEnum dar) {
        resetChoices();
        this.choice = Choice.DAR;
        this.dar = dar;
    }

    public void setData(Data data) {
        resetChoices();
        this.choice = Choice.DATA;
        this.data = data;
    }

    public Data getData() {
        return data;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        switch (this.choice) {
            case DAR:
                this.dar = new AxdrEnum();
                this.dar.setConst();
                codeLength += this.dar.decode(input);
                break;
            case DATA:
                this.data = new Data();
                codeLength += this.data.decode(input);
                break;
            default:
                throw new IllegalArgumentException("Error decoding AxdrChoice: Identifier matched to no item.");
        }
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            byte[] array = dataCode;
            for (int i = array.length - 1; i >= 0; i--) {
                output.write(array[i]);
            }
            return array.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new IllegalArgumentException("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        switch (this.choice) {
            case DATA:
                codeLength += this.data.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                break;
            case DAR:
                codeLength += this.dar.encode(output);
                c = new AxdrEnum();
                c.setMinMaxVal(0);
                codeLength += c.encode(output);
                break;
            default:
                throw new IllegalArgumentException("Error encoding AxdrChoice: No item in choice was encoded.");
        }
        return codeLength;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.dar = null;
        this.data = null;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    @Override
    public String toString() {
        switch (this.choice) {
            case DAR:
                return this.dar != null ? "choice: {dar: " + this.dar + "}" : "choice is dar but dar is null";
            case DATA:
                return this.data != null ? "choice: {data: " + this.data + "}" : "choice is data but data is null";
            default:
                return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        DAR(0),
        DATA(1);

        private final int value;

        Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



